import requests
from io import StringIO
import pandas as pd

def get_sina_hqs(stock_list):
    # type: (Union[str,list]) -> Optional[pd.DataFrame]

    if isinstance(stock_list, str):  # 单只股票str
        stock_list = [stock_list]

    # 股票代码：JQ格式 list 转 sina格式 str
    stock_list = [('sh' if x[-1] == 'G' else 'sz') + x[:6] for x in stock_list]
    sina_stocks = ','.join(stock_list)

    data_url = 'http://hq.sinajs.cn/list={}'.format(sina_stocks)
    r = requests.get(data_url)
    if not r.ok:
        return None

    rows = ';'.join(
        map(lambda x: x[13:19] + ('.XSHG,' if x[11:13] == 'sh' else '.XSHE,') + x[21:],
            r.text.strip('";\n').split('";\n'))
    )

    cols = ['name', 'open', 'pre_close', 'price', 'high', 'low', 'bid', 'ask', 'volume', 'amount',
            'b1_v', 'b1_p', 'b2_v', 'b2_p', 'b3_v', 'b3_p', 'b4_v', 'b4_p', 'b5_v', 'b5_p',
            'a1_v', 'a1_p', 'a2_v', 'a2_p', 'a3_v', 'a3_p', 'a4_v', 'a4_p', 'a5_v', 'a5_p',
            'date', 'time', 'status']

    df = pd.read_table(StringIO(rows), sep=',', lineterminator=';', header=None, names=cols)

    return df